//
//  NetWorkManager.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "NetWorkManager.h"
#import "NetWorkTool.h"
#import "Users.h"
#import "Disease.h"
#import "YJDoctorsIfonModel.h"
#import "WGCycleImageModel.h"
#import "Weather.h"
#import "WGComplicationModel.h"

@implementation NetWorkManager

/** 单例 */
+(instancetype)sharedManager{
    
    static NetWorkManager *instance = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        
        instance = [[NetWorkManager alloc] init];
        
    });
    return instance;
}

#pragma
#pragma mark - 1. 请求天气
/** 请求天气 */
- (void)loadWeatherWithCurrentCity:(NSString *)currentCity
                      successBlock:(void(^)(NSDictionary *cityDic))successBlock
                        errorBlock:(void(^)(NSString *error))errorBlock{
    
    
    if ([[NetWorkTool shareManager]isReachableByAFN]) {
        
        NSString *city = currentCity;
        NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
        
        NSDictionary *paramDic = @{@"location":city,
                                   @"output":@"json",
                                   @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                                   };
        
        [[NetWorkTool shareManager] GET_RequestWithUrlString:url parmas:paramDic successBloock:^(id responseObject) {
            
            NSInteger errorCode = [responseObject[@"error"] integerValue];
            
            if (errorCode == 0) {
                
                NSDictionary *dic = responseObject[@"results"][0][@"weather_data"][0];
                
                NSLog(@"dic");
                
                successBlock(dic);
                
            }else{
                
                NSLog(@"错误");
                
                NSLog(@"没有此城市数据");
            }
            
        } failureBlock:^(NSError *error) {
            
            NSLog(kUnKnownFailureStatus);
            
        }];
        
    }else{
        
        NSLog(kNetWorkFailureStatus);
        
    }
}

#pragma
#pragma mark -  2. 肿瘤疾病选择疾病细分列表接口
-(void)loadDiseaseSubWithCi_id:(int)ci_id
                          page:(int)page
                  successBlock:(void(^)(NSArray *dtoArray))successBlock
                    errorBlock:(void(^)(NSString *error))errorBlock{
    
    NSString *urlString = kSearchCI3Url;
    NSDictionary *paramDic = @{
                               @"ci1_id":@(ci_id),
                               @"page_size":@(page),
                               @"page":@1,
                               @"keyword":@""
                               };
    
    if ([[NetWorkTool shareManager] isReachableByAFN]) {
        
        [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
            
            NSLog(@"%@",responseObject);

        
            
        } failureBlock:^(NSError *error) {
            
            NSLog(@"error = %@",error);
        }];
        
    }else{
        
        errorBlock(kNetWorkFailureStatus);
    }
}

#pragma
#pragma mark -  3.登录
- (void)loginUserWihtAccount:(NSString *)account
                    password:(NSString *)password
                successBlock:(void(^)(Users *dto))successBlock
                  errorBlock:(void(^)(NSString *error))errorBlock{
    
    NSString *urlString = kLoginUrl;
    
    NSDictionary *paramDic = @{
                               @"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"
                               };
    
    
    if ([[NetWorkTool shareManager] isReachableByAFN]) {
        
        [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(NSData * responseObject) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL][@"data"];
            
            Users *user = [Users userWithDic:dic];
            
            successBlock(user);
            
        } failureBlock:^(NSError *error) {
            
            errorBlock(@"登录失败");
        }];
        
        
    }else{
        
        errorBlock(kNetWorkFailureStatus);
    }
    
}


#pragma
#pragma mark - 4.获取医生信息
- (void)loadDoctorsIfonWithParmas:(NSDictionary *)paramDic completeBlock:(void(^)(NSArray<YJDoctorsIfonModel *> *modelList,NSError *error))completeBlock errorBlock:(void(^)(NSString *error))errorBlock
{
    NSString *urlString = kMatchDoctors;
    
    if ([[NetWorkTool shareManager] isReachableByAFN]){
        
        [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
            
            NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            
            NSArray *stringArray = [str componentsSeparatedByString:@"<br />"];
            
            NSData *data = [stringArray[2] dataUsingEncoding:NSUTF8StringEncoding];
            
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
            
            NSArray *dictArray = dict[@"data"];
            
            NSArray<YJDoctorsIfonModel *>* modelList = [NSArray yy_modelArrayWithClass:[YJDoctorsIfonModel class] json:dictArray];
            
            completeBlock(modelList,nil);
            
            
        } failureBlock:^(NSError *error) {
            
            completeBlock(nil,error);
            
        }];
        
    }else{
        
        errorBlock(kNetWorkFailureStatus);
    }
    
}

#pragma
#pragma mark - 5. 获取图片轮播器
-(void)loadCycleImages:(void (^)(NSArray<WGCycleImageModel *> *images))completedBlock{
    
    NSString *bannerStr = kBannersUrl;
    if ([[NetWorkTool shareManager] isReachableByAFN]) {
        
        NSDictionary *paramDic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f",@"page_size":@(10),@"page":@(1)};
        [[NetWorkTool shareManager] POST_RequestWithUrlString:bannerStr parmas:paramDic successBloock:^(id responseObject) {
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            
            NSDictionary *bannerDict = dict[@"data"][@"banners"];
            
            
            NSArray<WGCycleImageModel *> *modelArray =  [NSArray yy_modelArrayWithClass:[WGCycleImageModel class] json:bannerDict];
            
            if (completedBlock) {
                
                completedBlock(modelArray);
            }
            
        } failureBlock:^(NSError *error) {
            
            
        } ];
    }
}



#pragma
#pragma mark - 6. 请求天气
-(void)loadWheatherWithCurrentCity:(NSString *)city Completed:(void (^)(NSArray<Weather *> *))completdeBlock{
    
    NSString *currentCity = city;
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
    
    NSDictionary *paramDic = @{@"location":currentCity,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };
    
    [[NetWorkTool shareManager] GET_RequestWithUrlString:url parmas:paramDic successBloock:^(id responseObject) {
    
         NSDictionary *dict =  [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL ];
        
        NSArray *array= dict[@"results"];
        
        NSArray  *dataArray = array[0][@"weather_data"] ;
        
        NSArray *modelArray = [NSArray yy_modelArrayWithClass:[Weather class] json:dataArray];
        
        if (completdeBlock) {
            
            completdeBlock(modelArray);
        }
        
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"天气错误= %@",error);
    }];
    
    
}


#pragma
#pragma mark - 7.疾病接口
-(void)loadDiseaseDataWithPageSize:(int)pageSize ci1_id:(int)ci1_id page:(int
                                                                         )page compete:(void(^)(NSArray<Disease *>* array, NSError *error))completedBlock{
    //    请求地址: http://iosapi.itcast.cn/searchCI3List.json.php
    //    请求参数:{"page":1,"page_size":15,"ci1_id":1,"keyword":""}
    
    NSString *urlString = kSearchCI3Url;
    NSDictionary *paramDic = @{
                               @"page_size":@(pageSize),
                               @"page":@(page),
                               @"ci1_id":@(ci1_id),
                               @"keyword":@""
                               };
    
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {

        id respond = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
      
        NSArray *array = [NSArray yy_modelArrayWithClass:[Disease class] json:respond[@"data"]];

        //数据加载成功
        completedBlock(array,nil);
        
        
    } failureBlock:^(NSError *error) {
        
        completedBlock(nil,error);
       
    }];
    
}

#pragma
#pragma mark - 8.选择并发症列表接口
//6.选择并发症列表接口
-(void)loadComlicationDataWithPage:(int)page pageSize:(int)pageSize ci2_id:(int)ci2_id completed:(void(^)(NSArray<WGComplicationModel *>* array,NSError *error))completedBlock{
    
    //    请求参数:{"page":1,"page_size":15,"ci2_id":3}
    NSString *urlString = kComplicationListUrl;
    NSDictionary *paramDic = @{@"page":@(page),
                               @"page_size":@(pageSize),
                               @"ci2_id":@(ci2_id)
                               };
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {

        
        id respond = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
    
        NSArray *dictArray = respond[@"data"];
        
      NSArray *array = [NSArray yy_modelArrayWithClass:[WGComplicationModel class] json:dictArray];
        
        if (completedBlock) {
            
            completedBlock(array,nil);
        }
        
        
    } failureBlock:^(NSError *error) {
        
       completedBlock(nil,error);
    }];
    
    
}



#pragma
#pragma mark - 9. 医生数量
-(void)loadDoctorCountWithC1_id:(int)c1_id  ci2_id:(int)ci2_id  ci3_id:(int)ci3_id completed:(void(^)(int count ))completedBalock{
    
    /*
     接口注意： 这些数据都是死的
     
     */
    
    NSString *urlString = kMatchedDoctorCountUrl;
    NSDictionary *paramDic = @{@"ci1_id":@(c1_id ),//1
                               @"ci2_id":@(ci2_id ),//3
                               @"ci3_id":@(ci3_id),//3
                               @"diagnosis_type":@0,
                               @"is_confirmed":@1,
                               @"user_id":@1000089,
                               @"has_diagnosis":@2
                               };
    
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
       id respond = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        int count = [respond[@"data"][@"doctor_count"] intValue];
        
        if (completedBalock) {
            
            //获取医生数量
             completedBalock(count);
        }
       
        
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
    
}



/** 最后统一测试的 */
-(void)loadUrl:(NSString *)urlString
        params:(NSDictionary *)paramDic{
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
        NSLog(@"responseObject = %@",responseObject);
        
     id respond = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
          NSLog(@"respond = %@",respond);
        
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
}

///** 关注医生接口 */
//#define kAddDoctorUrl @"http://iosapi.itcast.cn/addDoctor.json.php"
//
///** 取消关注医生接口 */
//#define kDeleteDoctorUrl @"http://iosapi.itcast.cn/deleteDoctor.json.php"
//
///** 获取关注医生列表接口 */
//#define kDoctorListUrl  @"http://iosapi.itcast.cn/doctorList.json.php"

#pragma mark -
#pragma mark - 获取关注医生列表

- (void)loadAttentionDoctorListWithUserID:(NSNumber *)usersId pageSize:(NSInteger)pageSize page:(NSInteger)page completeBlock:(void(^)(NSArray<YJDoctorsIfonModel *> *modelList,NSError *error))completeBlock errorBlock:(void(^)(NSString *error))errorBlock
{
    NSString *urlString = kDoctorListUrl;
    NSDictionary *parmas = @{@"user_id" :usersId,
                             @"page_size" :@(pageSize),
                             @"page" :@(page)};
    
    if ([[NetWorkTool shareManager] isReachableByAFN])
    {
        [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:parmas successBloock:^(id responseObject) {
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
            
            NSArray *dataArray = dict[@"data"];
            
            NSArray<YJDoctorsIfonModel *> *array = [NSArray yy_modelArrayWithClass:[YJDoctorsIfonModel class] json:dataArray];
            
            completeBlock(array,nil);
            
        } failureBlock:^(NSError *error) {
            
            completeBlock(nil,error);
        }];

    }else
    {
        errorBlock(kNetWorkFailureStatus);
    }
    
    
    
    
}




@end
